package com.zhx.real.advice.exception;


import cn.hutool.http.HttpStatus;
import com.zhx.real.core.util.Result;

import java.io.PrintWriter;
import java.io.StringWriter;

/**
 *describe: 
 *
 *@author zhanghaixuan
 *@date 2019/02/24
 **/
public class AbstractException extends RuntimeException {
	private static final long serialVersionUID = 1L;

	private final HttpStatus status;

	private final int code;

	private final String message;

	private final String detailMessage;

	public AbstractException(HttpStatus status, Result errorCode) {
		this.status = status;
		this.code = errorCode.getCode();
		this.message = errorCode.getMsg();
		this.detailMessage = errorCode.getData().toString();
	}

	public AbstractException(HttpStatus status) {
		this.status = status;
		code = 0;
		message = null;
		detailMessage = null;
	}

	public AbstractException(HttpStatus status, Result errorCode, Throwable cause) {
		super(cause);
		this.status = status;
		this.code = errorCode.getCode();
		this.message = errorCode.getData().toString();
		detailMessage = (cause.getMessage() != null ? cause.getMessage() : "") + toStackTrace(cause);
	}

	private String toStackTrace(Throwable e) {
		StringWriter errorStackTrace = new StringWriter();
		e.printStackTrace(new PrintWriter(errorStackTrace));
		return errorStackTrace.toString();

	}

	public HttpStatus getStatus() {
		return status;
	}

	public Result getCode() {
		return new Result(code,message,detailMessage);
	}

	@Override
	public String getMessage() {
		return message;
	}

	public String getDetailMessage() {
		return detailMessage;
	}
}
